<?php
App::uses('AppModel', 'Model');
/**
 * Product Model
 *
 */
class Product extends AppModel {

/**
 * Validation rules
 *
 * @var array
 */
	public $validate = array(
		'product_name' => array(
			'alphanumeric' => array(
				'rule' => array('alphanumeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
			'notempty' => array(
				'rule' => array('notempty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'price' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'in_stock' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
			'positive' => array(
				'rule' => array('validInStock'),
				'message' => 'in_stock must greater than 0'
				)
		),
	);
	
	public function validInStock($data) {
		return ($data['in_stock'] >= 0);
	}
	
	public function sub_in_stock($productId, $quantity, $transactionFlag = false) {
		$productInfo = $this->read(null, $productId);
		
		if (empty($productInfo)) {
			return 0;
		}
		
		$dataSouce = $this->getDataSource();
		
		// Truong hop su dung Transaction
		if ($transactionFlag) {
			$dataSouce->begin();
		}
		
		$result = $this->save(array(
			'id' => $productId,
			'in_stock' => $productInfo['Product']['in_stock'] - $quantity
			));
			
		// Truong hop su dung Transaction
		if ($transactionFlag) {
			if ($result) {
				$dataSouce->commit();
			} else {
				$dataSouce->rollback();
			}
		}
		
		return $result;
	}
}
